package cn.heantai.LBS;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Lesson26_GPSLocationActivity extends Activity {
	TextView tv1;
	Location location;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		// 定义UI组件
		Button b1 = (Button) findViewById(R.id.button1);
		tv1 = (TextView) findViewById(R.id.textView1);
 
		// 获取LocationManager对象
		LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 
		// 定义Criteria对象
		Criteria criteria = new Criteria();
		// 设置定位精确度 Criteria.ACCURACY_COARSE 比较粗略， Criteria.ACCURACY_FINE则比较精细
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 设置是否需要海拔信息 Altitude
		criteria.setAltitudeRequired(true);
		// 设置是否需要方位信息 Bearing
		criteria.setBearingRequired(true);
		// 设置是否允许运营商收费
		criteria.setCostAllowed(true);
		// 设置对电源的需求
		criteria.setPowerRequirement(Criteria.POWER_LOW);
 
		// 获取GPS信息提供者
		String bestProvider = lm.getBestProvider(criteria, true);
		Log.i("yao", "bestProvider = " + bestProvider);
 
		// 获取定位信息
		location = lm.getLastKnownLocation(bestProvider);
 
		// 给按钮绑定点击监听器
		b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				updateLocation(location);
			}
		});
 
		// 位置监听器
		LocationListener locationListener = new LocationListener() {
 
			// 当位置改变时触发
			@Override
			public void onLocationChanged(Location location) {
				Log.i("yao", location.toString());
				updateLocation(location);
			}
 
			// Provider失效时触发
			@Override
			public void onProviderDisabled(String arg0) {
				Log.i("yao", arg0);
 
			}
 
			// Provider可用时触发
			@Override
			public void onProviderEnabled(String arg0) {
				Log.i("yao", arg0);
			}
 
			// Provider状态改变时触发
			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				Log.i("yao", "onStatusChanged");
			}
		};
 
		// 500毫秒更新一次，忽略位置变化
		lm.requestLocationUpdates(bestProvider, 500, 0, locationListener);
 
	}
 
	// 更新位置信息
	private void updateLocation(Location location) {
		if (location != null) {
			tv1.setText("定位对象信息如下：" + location.toString() + "\n\t其中经度：" + location.getLongitude() + "\n\t其中纬度："
					+ location.getLatitude());
		} else {
			Log.i("yao", "没有获取到定位对象Location");
		}
	}

}